package io.mft.datasource.mq.function;

import org.junit.Test;

import cn.hutool.core.util.ObjectUtil;
import io.mft.api.domain.ExecutingResource;
import io.mft.api.domain.Regulation;
import io.mft.commons.exception.MFTException;
import io.mft.commons.function.ExecuteReturn;
import io.mft.commons.function.response.kv.KVMap;
import io.mft.datasource.mq.container.MqContainerManager;
import io.mft.datasource.mq.container.MqContainerType;
import io.mft.dev.base.BaseFunction;
import io.mft.dev.base.FunctionTester;
import io.mft.dev.base.MQMessageProducer.MQProducerType;
import io.mft.plugin.PAFunction;
import io.mft.plugin.PluginContext;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@PAFunction(value = "MQMessageProducer.publish", note = "消息队列生产者")
public class MqProducerFunction extends BaseFunction {

	@Override
	public ExecuteReturn doExecute(PluginContext<ExecutingResource<Regulation>> context) {
		KVMap message=context.getRequest().getRequestValue().getBean("message");
		String exchangeId=context.getRequest().getString("exchangeId", "");
		String pluginId=context.getRequest().getString("pluginId", "");
		if (ObjectUtil.isEmpty(exchangeId)) {
			throw new MFTException(-1, "[exchangeId]交换机\topic不允许为空,请检查");
		}
		if (ObjectUtil.isEmpty(pluginId)) {
			throw new MFTException(-1, "[pluginId]系统插件不允许为空,请检查");
		}
		MQProducerType mqProducerType=context.getRequest().getRequestValue().getBean("mqProducerType");
		MqContainerType mqContainerType=MqContainerType.valueOf(mqProducerType.name());
		MqContainerManager.getInstance().sendMessage(mqContainerType,pluginId, exchangeId, message);
		return ExecuteReturn.builder().build();
	}
    @Test
    public void test() {
        try {
            FunctionTester<MqProducerFunction> functionTester = new FunctionTester<>(MqProducerFunction.class);
    		KVMap kvMap=new KVMap();
    		kvMap.set("id", "012021042602");
            functionTester.addField("message", kvMap);
            functionTester.addField("mqProducerType", MQProducerType.rabbitmq);
            functionTester.addField("exchangeId", "testProducer");
            functionTester.addField("pluginId", "s0051");
            functionTester.testInvoke();
            log.info("");
        } catch (Throwable e) {
            log.error("", e);
        }

    }
}
